/**
 * @author yinghui.zhang 主界面
 */
 
// ------------------------------------------------- 密码验证
Ext.apply(Ext.form.VTypes, {
	checkpassword : function(val, field) {
		var newpass =  Ext.getDom('pwd').value;
		if(newpass != val){
			field.vtypeText = "两次输入密码不一致！" ;
			return false;
		}
		return true;
	}
});
 


Ext.ns("com.bangni.web.action.main");
var mainAdmin = com.bangni.web.action.MainAdmin;
var tipsSize=0;
var role='',accordion=[];

mainAdmin = function() {
	var topPanel, treePanel, menuPanel, mainPanel, userFormPanel, userWindow,bottomPanel;
	var frameUrl = cfg.cxt +  '/index.action';
	var arr_week = new Array("礼拜日", "礼拜一", "礼拜二", "礼拜三", "礼拜四", "礼拜五", "礼拜六");
	var curDate=new Date();
	week = arr_week[curDate.getDay()];//获取当天日期是星期几
	var month = curDate.getMonth() + 1;//当前月份
	var date=curDate.getDate();//当前天数
	if (month < 10) month = "0" + month;
	if (date < 10) date = "0" + date;
	Ext.get('sp_line').dom.innerHTML='今天是 ：'+curDate.getFullYear()+'-'+month+'-'+date+ ' '+week; 
	
	return {
		init : function() {
			// ------------------------------------------------- 头部
			topPanel = this.createTopPanel();
			
			// ------------------------------------------------- 左侧区域
			menuPanel  = this.createMenuPanel();
			// ------------------------------------------------- 主区域
			mainPanel = this.createMainPanel();
			
			// ------------------------------------------------- 尾部区域
			bottomPanel=this.createFootPanel();
			

			// ------------------------------------------------- viewport
			new Ext.Viewport({
				layout: 'border',
				items: [topPanel,menuPanel,mainPanel,bottomPanel]
			});
			
			// ------------------------------------------------- 修改用户资料
			userFormPanel = this.createUserFormPanel();
			userWindow = this.createUserWindow();
			userWindow.on('show', function() {
			userFormPanel.getForm().reset();
			userFormPanel.getForm().setValues(user);
			//Ext.getCmp('accountName').setValue(Ext.getCmp('loginName').getValue());
			});
		},
			//-------------------createTopPanel--------------------------
			createTopPanel: function() {
				return new Ext.Panel( {
							border: false,
							region:'north',
							contentEl:'header1',
							height:87
						});
			},
			//-------------------createFootPanel--------------------------
			createFootPanel: function() {
				return new Ext.Panel( {
							border: false,
							region:'south',
							contentEl:'bottom',
							height:25
						});
			},
			//-------------------createMenuPanel--------------------------
			createMenuPanel:function(){
				return new Ext.Panel({
					border : false,
					region: 'west',
					contentEl:'left_all',
					collapseMode : "mini",
					bodyStyle : 'background:#d6e6f6;',
					split: true,
					width: 200
				   });
			},
			//-------------------createMainPanel--------------------------
			createMainPanel : function() {
				return  new Ext.TabPanel({
								region:'center',
								activeTab:0,
								id:'mainPanel',
								enableTabScroll:true,
								plugins: new Ext.ux.TabCloseMenu(),
								items:[{
									title:"首页",
									autoScroll : false,
									id:"work",
									html:"<iframe name='mainFrame' id='mainFrame' src="+ frameUrl +" scrolling='no' frameborder='0' width='100%' height='100%' ></iframe>"
								}],
							    listeners : {
									"add" : function(d, b, c) {//最多打开8个
					                   
										if (d.items.length > 8) {
											d.remove(d.items.get(1)); //首页不要清除
											d.doLayout();
										}
									}
//								   
								}
					 });
			},
			addNewTab : function(nodeId,nodeText,nodeUrl,refresh,pnodeText){
				
				var id = "tab_id_"+ nodeId;
				var tabTitle = pnodeText +'>'+nodeText;
				var src =nodeUrl;
				
				if(!src || src == '#' || src == ''){
		             Ext.Msg.alert('提示', '此菜单还没有指定请求地址,无法为您打开页面！');
		             return;
				}
				var n = mainPanel.getComponent(id);
				if(!n){
					if (Ext.isIE6) {
						mainPanel.add({
							title : tabTitle,
							id : id,
							autoScroll : false,
							closable : true,
							html : '<iframe src="" id="iframe_' + id + '" scrolling="no" frameborder="0" width="100%" height="100%"></iframe>'
						}).show();
						Ext.get('iframe_' + id).dom.src =  src + "?_n="+Math.random();
					} else {
						mainPanel.add({
							layout : 'fit',
							deferredRender : false, //是否延迟渲染
							id : id,
							title : tabTitle,
							autoScroll : false,
							closable : true,
							html : String.format('<iframe src={0} id="iframe_' + id
											     + '" scrolling="no" frameborder="0" width="100%" height="100%"></iframe>', src+ "?_n="+Math.random())
						}).show();
					}
				} else {
					mainPanel.setActiveTab(n);
					if (refresh) {
						Ext.get('iframe_' + id).dom.src =  src+ "?_n="+Math.random();
				}
			} 	
		},
		//-------------------------------------------------------以tab方式打开新的页面----------------------------------------------------------------------------
       addNewTab : function(nodeId,nodeText,nodeUrl,refresh,pnodeText){
			
			var id = "tab_id_"+ nodeId;
			var tabTitle = pnodeText +'>'+nodeText;
			var src =cfg.cxt+nodeUrl;
			
			if(!src || src == '#' || src == ''){
	             Ext.Msg.alert('提示', '此菜单还没有指定请求地址,无法为您打开页面！');
	             return;
			}
			var n = mainPanel.getComponent(id);
			if(!n){
				if (Ext.isIE6) {
					mainPanel.add({
						title : tabTitle,
						id : id,
						autoScroll : false,
						closable : true,
						html : '<iframe src="" id="iframe_' + id + '" scrolling="no" frameborder="0" width="100%" height="100%"></iframe>'
					}).show();
					Ext.get('iframe_' + id).dom.src =  src + "?_n="+Math.random();
				} else {
					mainPanel.add({
						layout : 'fit',
						deferredRender : false, //是否延迟渲染
						id : id,
						title : tabTitle,
						autoScroll : false,
						closable : true,
						html : String.format('<iframe src={0} id="iframe_' + id
										     + '" scrolling="no" frameborder="0" width="100%" height="100%"></iframe>', src+ "?_n="+Math.random())
					}).show();
				}
			} else {
				mainPanel.setActiveTab(n);
				if (refresh) {
					Ext.get('iframe_' + id).dom.src =  src+ "?_n="+Math.random();
			}
		} 	
	},
		
		loginOut : function(){
				Ext.MessageBox.confirm('提示', '您确定要退出本系统吗？', function(btn) {
					if (btn == 'yes') {
						window.location.href = cfg.cxt+'/user/logOut.action';
					}
				});
		
		
		},
		createUserFormPanel : function() {
			return new Ext.form.FormPanel( {
							defaultType : 'textfield',
							labelAlign : 'right',
							labelWidth : 65,
							frame : false,
							padding : 10,
							items : [ {
								fieldLabel : '登录帐户',
								name : 'loginName',
								id : 'loginName',
								readOnly:true,
								anchor : '99%'
							}, {
								fieldLabel : '登录姓名',
								name : 'name',
								id : 'name',
								readOnly:true,
								allowBlank : false,
								anchor : '99%'
							}, {
								fieldLabel : '初始密码',
								name : 'pwd',
								id : 'pwd',
								inputType : 'password',
								allowBlank : false,
								anchor : '99%'
							}, {
								fieldLabel : '确认密码',
								name : 'pwdConfig',
								id : 'pwdConfig',
								inputType : 'password',
								vtype : 'checkpassword',
								allowBlank : false,
								anchor : '99%'
							}, {
								id : 'id',
								name : 'id',
								hidden : true
							} ]
					});
		},
		
		createUserWindow : function() {
			 return new Ext.Window({
							layout : 'fit',
							width : 300,
							height : 275,
							resizable : false,
							draggable : true,
							closeAction : 'hide',
							id:'userwin',
							modal : true,
							title : '<span style="font-weight:normal">修改帐户信息</span>',
							iconCls : 'configIcon',
							collapsible : true,
							titleCollapse : true,
							maximizable : false,
							buttonAlign : 'right',
							border : false,
							animCollapse : true,
							animateTarget : Ext.getBody(), //是否有蒙板效果
							constrain : true,
							items : [ userFormPanel ],
							buttons : [ {
								text : '保存',
								iconCls : 'acceptIcon',
								handler : function() {
									var f = userFormPanel.getForm();
									if (f.isValid()) {
										userFormPanel.form.submit( {
											url : cfg.cxt + '/user/updatePwd.action',
											waitTitle : '提示',
											method : 'POST',
											waitMsg : '正在处理数据,请稍候...',
											success : function(form, action) {
												userWindow.hide();
												Ext.Msg.confirm('提示',
														'修改成功，需要重新登录，确认重新登录本系统吗？',
														function(btn, text) {
															if (btn == 'yes') {
																 window.location.href = cfg.cxt+'/user/logOut.action'
															}
												});
											}
										});
									}
								}
							}, {
								text : '关闭',
								iconCls : 'deleteIcon',
								handler : function() {
									userWindow.hide();
								}
							} ]
					});
		},
		
		updateUserInit : function() {
				userWindow.show();
	   },
	   forwardToIndex : function(){
		   mainPanel.setActiveTab(mainPanel.getComponent(0));
	   }	
				
		}
}();
Ext.onReady(mainAdmin.init, mainAdmin);